package test.hk.ust.cse.TwitterClient;

import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;

import java.net.MalformedURLException;
import java.net.URL;

import hk.ust.cse.TwitterClient.Utils;
import hk.ust.cse.TwitterClient.Resources.Resources;
import hk.ust.cse.TwitterClient.Views.ControlBar;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Widget;
import org.junit.Test;

public class UtilsTest {
	@Test(timeout=10000)
	  public void testConstructor() throws Throwable {
		Display display = new Display();
	    Shell shell = new Shell(display);
	    Utils u = new Utils();
		Event event = new Event(); 
		event.widget = shell; 
		MouseEvent e = new MouseEvent(event); 	//u.notifyListeners(SWT.MouseDown, e);	
		shell.dispose();
	    display.dispose();
  }
	  
	
  @Test(timeout=10000)
  public void testGetColorFromString() throws Throwable {
      assertEquals(new Color(null, 171, 205, 239), Utils.getColorFromString("ABCDEF"));
      assertFalse(Utils.isNullOrEmpty("abc"));
      assertTrue(Utils.isNullOrEmpty(""));
      assertTrue(Utils.isNullOrEmpty(null));
      assertNotNull(Utils.loadImageFromUrl("https://www.google.com.hk/images/nav_logo117.png"));
      assertNull(Utils.loadImageFromUrl(""));
  }
  
  @Test(timeout=10000)
  public void testloadImageFromUrl() throws Throwable{
	    Image image = null;
	    String url = "https://www.google.com.hk/images/nav_logo117.png";
	      ImageDescriptor imageDesc = ImageDescriptor.createFromURL(new URL(url));
	      image = imageDesc.createImage();
	      assertNotNull(image);
	   // assertEquals(image, Utils.loadImageFromUrl("https://www.google.com.hk/images/nav_logo117.png"));
	      assertNotNull(Utils.loadImageFromUrl("http://www.bm.ust.hk/bm/HighResImages/Copy2%20of%20sundial_M0090_01_009.JPG"));
	      
  }
  
  @Test(timeout=10000)
  public void testloadImageFromUrlAndScale() {
	 //Image img =  Utils.loadImageFromUrlAndScale("https://www.google.com.hk/images/nav_logo117.png", 10, 10);
	 //assertNotNull(img);
	 //assertEquals(10, Utils.loadImageFromUrlAndScale("https://www.google.com.hk/images/nav_logo117.png", 10, 10).getBounds().width);
	 //assertEquals(10, Utils.loadImageFromUrlAndScale("https://www.google.com.hk/images/nav_logo117.png", 10, 10).getBounds().height);
	  assertNull(Utils.loadImageFromUrlAndScale("",10,8));
	  Image image = Utils.loadImageFromUrlAndScale("https://www.google.com.hk/images/nav_logo117.png", 167, 389);
	  int width1 = image.getBounds().width;
	  int height1 = image.getBounds().height;	
	  assertEquals(167,width1);
	  assertEquals(389,height1);
	  assertNotNull(image);
	  Image image2 = Utils.loadImageFromUrlAndScale("http://www.taopic.com/uploads/allimg/100321/9-1003210204150.jpg", 167, 389);
	  int width = image2.getBounds().width;
	  int height = image2.getBounds().height;	
	  assertEquals(167,width);
	  assertEquals(389,height);
	  
	  //////////////////////////////
	  int width_true = 1440,height_true = 960;
	    assertNotNull(Utils.loadImageFromUrlAndScale("http://img407.imageshack.us/img407/2236/sujusorry21.jpg",width_true,height_true));
	    int width_false = 300, height_false = 200;
	    assertNotNull(Utils.loadImageFromUrlAndScale("http://img407.imageshack.us/img407/2236/sujusorry21.jpg",width_false,height_false));
	    assertNotNull(Utils.loadImageFromUrlAndScale("http://img407.imageshack.us/img407/2236/sujusorry21.jpg",width_true,height_false));
	    assertNull(Utils.loadImageFromUrlAndScale(null, width_true, height_false));
	  
	  
	  //////////////////////////////
	     Image img1 = Utils.loadImageFromUrlAndScale("http://www.cse.ust.hk/~hunkim/images/Sungkim2.png",147,200);	 
		 Image img2 = Utils.loadImageFromUrlAndScale("http://www.cse.ust.hk/~hunkim/images/Sungkim2.png",2,2);
		 Image img3 = Utils.loadImageFromUrlAndScale("",1,1);
		 Image img4 = Utils.loadImageFromUrlAndScale("http://www.cse.ust.hk/~hunkim/images/Sungkim2.png",3,200);
		 Image img5 = Utils.loadImageFromUrlAndScale("http://www.cse.ust.hk/~hunkim/images/Sungkim2.png",147,3);
		 
		 if (Display.getCurrent() != null) {
		      Display.getCurrent().dispose();
		  }
	  ///////////////////////////////
	  
  }
  
  @Test(timeout=10000)
  public void testisNullOrEmpty() {
	   assertFalse(Utils.isNullOrEmpty("abc"));
	   assertTrue(Utils.isNullOrEmpty(null));
	   assertFalse(Utils.isNullOrEmpty("123"));
	   assertTrue(Utils.isNullOrEmpty(""));
  }
  
 /* public void selectImageVersion() {
	  String str = Utils.selectImageVersion("https://www.google.com.hk/images/nav_logo117.png", 10, 10);
	  assertNotNull(str);
	  assertEquals(str, "https://www.google.com.hk/images/nav_logo117.png");
  }*/
  
  
  @Test(timeout=10000)
  public void testselectImageVersion()
  {	  
	  Image image1 = Utils.loadImageFromUrlAndScale("https://www.google.com.hk/images/nav_logo117.png", 167, 389);	  
	  assertNotNull(image1);
	  Utils.darkGradually(image1 , 100);
	  Image image2 = Utils.loadImageFromLocal(Resources.class, "./people2.png"); 
	  assertNotNull(image2);
	  Utils.darkGradually(image2 , 100);
	  
	  
	  /////////////////////////
	  Image image = Utils.loadImageFromUrl("http://dc406.4shared.com/img/99rQ_n9A/s3/24_bit_png.png");
	  assertNotNull(Utils.darkGradually(image, 0));
	  Image image3 = Utils.loadImageFromUrl("http://img407.imageshack.us/img407/2236/sujusorry21.jpg");
	  assertNotNull(Utils.darkGradually(image3, 0));
	  
	  /////////////////////////
	  assertNotNull(Utils.selectImageVersion("http://abc.com/_normal.png", 20, 20));
		assertNotNull(Utils.selectImageVersion("http://abc.com/_normal.png", 50, 50));
		assertNotNull(Utils.selectImageVersion("http://abc.com/_normal.png", 100, 100));
		assertNotNull(Utils.selectImageVersion("http://abc.com/_bigger.png", 20, 50));
		assertNotNull(Utils.selectImageVersion("http://abc.com/_bigger.png", 50, 70));
		assertNotNull(Utils.selectImageVersion("http://abc.com/_bigger.png", 70, 100));
		assertNotNull(Utils.selectImageVersion("http://abc.com/_fff.png", 20, 20));

	  ////////////////////////
  }
  
  @Test(timeout=10000)
  public void testdarkGradually()
  {	  	  
	  String str1 = Utils.selectImageVersion("test_normal.png", 10, 10);
	  String str2 = Utils.selectImageVersion("test1_normal.png",200,200);
	  String str3 = Utils.selectImageVersion("next_bigger.png",50,50);
	  String str4 = Utils.selectImageVersion("next_bigger.png",200,200);	  
	  String str5 = Utils.selectImageVersion("null",10,10);
	  String str6 = Utils.selectImageVersion("next_bigger.png",10,100);
	  String str7 = Utils.selectImageVersion("next_bigger.png",100,10);	  
  }
  
  @Test
  public void testCutRoundCorner() throws Throwable {
	  Display display = new Display();
	  Shell shell = new Shell(display);
	  Utils.cutRoundCorner(shell, true, true, true, true);
	  Utils.cutRoundCorner(shell, false, false, false, false);
	  //shell.dispose();
	  if (Display.getCurrent() != null) {
	      Display.getCurrent().dispose();
	  }
  }
  
  
  @Test(timeout=10000)
  public void testdispose()
  {	  	  
	    Display display = new Display();
	    Shell shell = new Shell(display);
	    Event a = new Event();
	    a.widget = shell;
	    Widget wiget = a.widget;
	    Utils.dispose(wiget);
	    shell.dispose();
	    display.dispose();
  }
  
}
